<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="股票">
        <el-select
          v-model="queryParams.symbol"
          filterable
          remote
          placeholder="请输入股票名称或代码"
          @change="getList"
          :remote-method="getStockOptions"
          :loading="optionsLoading"
          default-first-option
        >
          <el-option
            v-for="item in selectOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="交易日期" prop="tradeDate">
        <el-date-picker
          v-model="queryParams.tradeDate"
          type="date"
          value-format="yyyyMMdd"
          format="yyyy 年 MM 月 dd 日"
          placeholder="请选择交易日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="dailyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="股票代码" align="center" prop="symbol" />
      <el-table-column label="股票名称" align="center" prop="name" />
      <el-table-column label="市场类型" align="center" prop="market" />
      <el-table-column label="收盘价" align="center" prop="close" />
      <el-table-column label="涨跌额" align="center" prop="change" />
      <el-table-column label="涨跌幅" align="center" prop="pctChg" sortable>
        <template slot-scope="scope">
          <font v-if="scope.row.pctChg > 0" color="red"
            >{{ scope.row.pctChg }}%</font
          >
          <font v-else color="green">{{ scope.row.pctChg }}%</font>
        </template>
      </el-table-column>
      <el-table-column label="开盘价" align="center" prop="open" />
      <el-table-column label="最高价" align="center" prop="high" />
      <el-table-column label="最低价" align="center" prop="low" />
      <el-table-column label="昨收价" align="center" prop="preClose" />
      <el-table-column label="成交量（手）" align="center" prop="vol" />
      <el-table-column
        label="成交额（千）"
        align="center"
        prop="amount"
        :formatter="amountFormatter"
      />
      <el-table-column label="交易日期" align="center" prop="tradeDate" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改股票日线行情信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="TS代码" prop="tsCode">
          <el-input v-model="form.tsCode" placeholder="请输入TS代码" />
        </el-form-item>
        <el-form-item label="股票代码" prop="symbol">
          <el-input v-model="form.symbol" placeholder="请输入股票代码" />
        </el-form-item>
        <el-form-item label="股票名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入股票名称" />
        </el-form-item>
        <el-form-item label="市场类型" prop="market">
          <el-input v-model="form.market" placeholder="请输入市场类型" />
        </el-form-item>
        <el-form-item label="开盘价" prop="open">
          <el-input v-model="form.open" placeholder="请输入开盘价" />
        </el-form-item>
        <el-form-item label="最高价" prop="high">
          <el-input v-model="form.high" placeholder="请输入最高价" />
        </el-form-item>
        <el-form-item label="最低价" prop="low">
          <el-input v-model="form.low" placeholder="请输入最低价" />
        </el-form-item>
        <el-form-item label="收盘价" prop="close">
          <el-input v-model="form.close" placeholder="请输入收盘价" />
        </el-form-item>
        <el-form-item label="昨收价" prop="preClose">
          <el-input v-model="form.preClose" placeholder="请输入昨收价" />
        </el-form-item>
        <el-form-item label="涨跌额" prop="change">
          <el-input v-model="form.change" placeholder="请输入涨跌额" />
        </el-form-item>
        <el-form-item label="涨跌幅" prop="pctChg">
          <el-input v-model="form.pctChg" placeholder="请输入涨跌幅" />
        </el-form-item>
        <el-form-item label="成交量" prop="vol">
          <el-input v-model="form.vol" placeholder="请输入成交量" />
        </el-form-item>
        <el-form-item label="成交额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入成交额" />
        </el-form-item>
        <el-form-item label="交易日期" prop="tradeDate">
          <el-input v-model="form.tradeDate" placeholder="请输入交易日期" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDaily,
  getDaily,
  delDaily,
  addDaily,
  updateDaily,
  exportDaily,
} from "@/api/stock/daily";
import { listBasic } from "@/api/stock/basic";

export default {
  name: "Daily",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股票日线行情信息表格数据
      dailyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      optionsLoading: false,
      selectOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tsCode: null,
        symbol: null,
        name: null,
        market: null,
        open: null,
        high: null,
        low: null,
        close: null,
        preClose: null,
        change: null,
        pctChg: null,
        vol: null,
        amount: null,
        tradeDate: null,
      },
      optionsQueryParams: {
        keyword: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.queryParams.tradeDate = this.moment().format("YYYYMMDD");
    this.getList();
  },
  methods: {
    /** 查询股票日线行情信息列表 */
    getList() {
      this.loading = true;
      debugger;
      listDaily(this.queryParams).then((response) => {
        this.dailyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tsCode: null,
        symbol: null,
        name: null,
        market: null,
        open: null,
        high: null,
        low: null,
        close: null,
        preClose: null,
        change: null,
        pctChg: null,
        vol: null,
        amount: null,
        tradeDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加股票日线行情信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDaily(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改股票日线行情信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDaily(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDaily(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除股票日线行情信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDaily(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有股票日线行情信息数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportDaily(queryParams);
        })
        .then((response) => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    amountFormatter(row, column) {
      return row.amount;
    },
    getStockOptions(query) {
      if (query != "" && query.length > 1) {
        this.optionsLoading = true;
        this.optionsQueryParams.keyword = query;
        debugger;
        listBasic(this.optionsQueryParams).then((response) => {
          this.optionsLoading = false;
          this.selectOptions = [];
          this.selectOptions = response.rows.map((item) => {
            return {
              value: `${item.symbol}`,
              label: `${item.name} - ${item.symbol}`,
            };
          });
        });
      }
    },
  },
};
</script>
